package org.langqiao.io.file;

import java.io.*;

/**
 * 将Fiboncci序列值写入文本文件中
 * @author YMY
 */
public class TextFile {
	
	private String filename;//指定文件名
	
	public TextFile(String filename){
		this.filename = filename;//指定文件名
	}
	
	//将Fiboncci写入到指定文本文件
	public void writeToText()throws IOException {
		FileWriter fout = new FileWriter(this.filename);
		short i=0,j=1,count=0;
		do{
			fout.write(i+" "+j+" ");//向文件字符输出流写入一个字符窜
			i=(short)(i+j);
			j=(short)(i+j);
			count+=2;
			if(count%20==0)
				fout.write("\r\n");//写入一个回车换行符			
		}while(i>0);
		fout.close();//关闭文件字符输出流
	}
	
	//从指定文件中读取字符窜
	public void readFromText()throws IOException {
		FileReader fin = new FileReader(this.filename);
		BufferedReader bin = new BufferedReader(fin);
		System.out.println(this.filename+":");
		String aline = "";
		do{
			aline = bin.readLine();//读取一行字符窜，输入流结束时返回null
			if(aline != null)
				System.out.print(aline);
		}while(aline != null);
		bin.close();
		fin.close();
	}
	
	public static void main(String[] args)throws IOException {
		TextFile afile = new TextFile("FibFile.txt");
		afile.writeToText();
		afile.readFromText();
	}
}
